import redis
from cacheout import Cache


class CacheKit(object):

    def __init__(self,key=None,ttl = None,config=None):
        if key is None:
            self.cache = Cache(ttl=ttl)
        elif key == 'redis':
            pool = redis.ConnectionPool(host='10.2.111.66', port=6379, db=2,
                                        decode_responses=True)  # host是redis主机，需要redis服务端和客户端都起着 redis默认端口是6379
            self.cache = redis.Redis(connection_pool=pool)
        self.ttl = ttl

    @staticmethod
    def get_cache():
        return self.cache

    @staticmethod
    def set_cache(caches):
        CacheKit.cache = caches

    def get(self,key):
        return CacheKit.cache.get(key)

    def test(self):
        pass

class Cac(CacheKit):

    def __init__(self):
        self.cache = Cache()
        self.cache.set('a', '1')

    def test(self):
        return self.cache.get('a')

class CacR(CacheKit):
    config = None

    def __init__(self):
        pool = redis.ConnectionPool(host='10.2.111.66', port=6379, db=2,
                                    decode_responses=True)  # host是redis主机，需要redis服务端和客户端都起着 redis默认端口是6379
        self.cache = redis.Redis(connection_pool=pool)
        self.cache.set('a', '2')

    def test(self):
        return self.cache.get('a')


if __name__ == '__main__':
    c = CacR()
    print( c.config)
    c.config = 'a'
    print(c.config)
    d = CacR()
    print(d.config)
    CacR.config = 'b'
    d.config = 'c'
    print(d.config)
    print(c.config)
    print(CacR.config)